package com.bruceliu.controller;

import com.bruceliu.bean.Desk;
import com.bruceliu.service.DeskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @BelongsProject: RestaurantSystem
 * @BelongsPackage: com.bruceliu.controller
 * @Author: bruceliu
 * @QQ:1241488705
 * @CreateTime: 2020-02-29 10:30
 * @Description: TODO
 */
@Controller
@Scope("prototype")
public class DeskController {

    @Autowired
    DeskService deskService;

    @RequestMapping("/desk_query")
    public String query(Desk desk, Model model) {
        System.out.println("查询参数是:" + null);
        List<Desk> desks = deskService.getDesks(desk);
        System.out.println("查询结果:" + desks);
        model.addAttribute("desks", desks);
        //回显搜索内容
        model.addAttribute("desk", desk);
        return "deskList";
    }

    @RequestMapping("/desk_add")
    public void addDesk(Desk desk, HttpServletResponse response) throws Exception {
        //处理响应的时候的中文乱码问题
        response.setContentType("text/html;charset=utf-8");
        int count = deskService.addDesk(desk);
        if (count > 0) {
            response.getWriter().write("<script>alert('餐桌新增成功!');location.href='/desk_query';</script>");
        }else{
            response.getWriter().write("<script>alert('餐桌新增失败!');location.href='/page_saveDesk';</script>");
        }
    }

    @RequestMapping("/desk_delete/{id}")
    public void deleteDesk(@PathVariable("id") int id,HttpServletResponse response) throws Exception {
        response.setContentType("text/html;charset=utf-8");
        int count = deskService.deleteDesk(id);
        if (count > 0) {
            response.getWriter().write("<script>alert('餐桌删除成功!');location.href='/desk_query';</script>");
        }else{
            response.getWriter().write("<script>alert('餐桌删除失败!');location.href='/desk_query';</script>");
        }
    }

    @RequestMapping("/desk_changeState/{id}/{state}")
    public void changeState(@PathVariable("id") int id,@PathVariable("state") int state,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        Desk desk=new Desk();
        desk.setId(id);
        desk.setState(state);
        if(state==1){
            desk.setBooktime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }else{
            desk.setBooktime(null);
        }
        int count = deskService.changeState(desk);
        String message="";
        if (count > 0) {
            if(state==1){
                message="餐桌预定成功！";
            }else{
                message="餐桌退桌成功！";
            }
            response.getWriter().write("<script>alert('"+message+"');location.href='/desk_query';</script>");
        }else{
            if(state==1){
                message="餐桌预定失败！";
            }else{
                message="餐桌退桌失败！";
            }
            response.getWriter().write("<script>alert('"+message+"');location.href='/desk_query';</script>");
        }
    }

    /**
     * 进入前台点餐系统
     * @param model
     * @return
     */
    @RequestMapping({"/main","/"})
    public String getDesks(Model model){
        List<Desk> desks = deskService.getDesks(null);
        model.addAttribute("desks",desks);
        return "main";
    }

}
